package com.atjava.webspider.gui.output;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.commons.lang.StringUtils;

import com.atjava.webspider.gui.site.NewSiteGui;
import com.atjava.webspider.model.Page;
import com.atjava.webspider.model.Site;
import com.atjava.webspider.model.Variable;
import com.atjava.webspider.services.TableService;
import com.atjava.webspider.util.DialogUtil;

public class NewVarGui extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JLabel jLabelKey = null;
	private JLabel jLabelGroupNum = null;
	private JLabel jLabelOwner = null;
	private JLabel jLabelOwnerText = null;
	private JLabel jLabelRegexp = null;
	private JTextField jTextFieldKey = null;
	private JTextField jTextFieldGroupNum = null;
	private JButton jButtonSave = null;
	private JButton jButtonReset = null;
	private JScrollPane jScrollPane = null;
	private JTextArea jTextAreaRegexp = null;
	private Variable var = null;
	
	/**
	 * @param owner
	 */
	public NewVarGui(JDialog owner,boolean modal) {
		this(owner,modal,new Variable());
	}
	
	public NewVarGui(JDialog owner,boolean modal,Variable var) {
		super(owner,modal);
		this.var = var;
		initialize();
		setTextValue();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setBounds(new Rectangle(0, 0, 505, 214));
		this.setResizable(false);
		this.setTitle("新建变量");
		this.setLocationRelativeTo(getOwner());
		this.setContentPane(getJContentPane());
	}
	
	private void setTextValue(){
		if(var == null)
			return ;
		try {
			if(StringUtils.isNotBlank(var.getName())){
				jTextFieldKey.setText(var.getName());
				jTextFieldKey.setEnabled(false);
			}
			
			if(var.getGroupNum() != 0)
				jTextFieldGroupNum.setText(String.valueOf(var.getGroupNum()));
			
			if(StringUtils.isNotBlank(var.getRegexp()))
				jTextAreaRegexp.setText(var.getRegexp());
			
			//设置当前输出名称
			NewSiteGui newSiteGui = (NewSiteGui)NewVarGui.this.getOwner();
			Site st = newSiteGui.getSite();
			JTable jTableOutput = newSiteGui.getJTablePage();
			//获得所选输出
			Page p = st.getPages().get(jTableOutput.getSelectedRow());
			jLabelOwnerText.setText(p.getName());
		} catch (Exception e) {
			e.printStackTrace();
			DialogUtil.showError(this, "加载变量失败！");
		}
	}
	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabelKey = new JLabel();
			jLabelKey.setBounds(new Rectangle(5, 5, 65, 25));
			jLabelKey.setText("变量名称：");
			
			jLabelOwner = new JLabel();
			jLabelOwner.setBounds(new Rectangle(5, 34, 65, 25));
			jLabelOwner.setText("所属输出：");
			
			jLabelOwnerText = new JLabel();
			jLabelOwnerText.setBounds(new Rectangle(70, 34, 420, 25));
			jLabelOwnerText.setText("");
			
			jLabelRegexp = new JLabel();
			jLabelRegexp.setBounds(new Rectangle(5, 63, 65, 25));
			jLabelRegexp.setText("取值规则：");
			
			jLabelGroupNum = new JLabel();
			jLabelGroupNum.setBounds(new Rectangle(377, 5, 65, 25));
			jLabelGroupNum.setText("取值组号：");
			
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabelKey, null);
			jContentPane.add(jLabelRegexp, null);
			jContentPane.add(jLabelGroupNum, null);
			jContentPane.add(jLabelOwner, null);
			jContentPane.add(jLabelOwnerText, null);
			
			jContentPane.add(getJTextFieldKey(), null);
			jContentPane.add(getJTextFieldGroupNum(), null);
			
			jContentPane.add(getJButtonSave(), null);
			jContentPane.add(getJButtonReset(), null);
			jContentPane.add(getJScrollPane(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTextFieldName	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldKey() {
		if (jTextFieldKey == null) {
			jTextFieldKey = new JTextField();
			jTextFieldKey.setBounds(new Rectangle(70, 5, 265, 25));
			jTextFieldKey.setFont(new Font("Courier New", Font.PLAIN, 12));
		}
		return jTextFieldKey;
	}

	/**
	 * This method initializes jTextFieldGroupNum	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldGroupNum() {
		if (jTextFieldGroupNum == null) {
			jTextFieldGroupNum = new JTextField();
			jTextFieldGroupNum.setBounds(new Rectangle(442, 5, 49, 25));
			jTextFieldGroupNum.setText("1");
		}
		return jTextFieldGroupNum;
	}

	/**
	 * This method initializes jButtonSave	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonSave() {
		if (jButtonSave == null) {
			jButtonSave = new JButton();
			jButtonSave.setBounds(new Rectangle(342, 155, 70, 25));
			jButtonSave.setText("保存");
			jButtonSave.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try {
						NewSiteGui newTaskGui = (NewSiteGui)NewVarGui.this.getOwner();
						Site st = newTaskGui.getSite();
						//获得父窗口输出Table
						JTable jTablePage = newTaskGui.getJTablePage();
						//获得所选输出
						Page page = st.getPages().get(jTablePage.getSelectedRow());
						if(page.getVariables() == null)
							page.setVariables(new ArrayList<Variable>());
						//创建变量
						var.setGroupNum(Integer.parseInt(jTextFieldGroupNum.getText()));
						var.setName(jTextFieldKey.getText());
						var.setRegexp(jTextAreaRegexp.getText());
						
						TableService tabService = TableService.getInstance();
						int idx = isInList(page.getVariables());
						if(idx >= 0){
							page.getVariables().set(idx, var);
							newTaskGui.getVarModel().updateData(idx, tabService.getVarRow(var));
						}else{
							page.getVariables().add(var);
							newTaskGui.getVarModel().addData(tabService.getVarRow(var));
						}
						
						NewVarGui.this.setVisible(false);
					} catch (Exception e2) {
						e2.printStackTrace();
					}
				}
			});
		}
		return jButtonSave;
	}
	
	private int isInList(List<Variable> variables){
		for (int i = 0; i < variables.size(); i++) {
			Variable v = variables.get(i);
			if(v.getName().equals(var.getName())){
				return i;
			}
		}
		return -1;
	}

	/**
	 * This method initializes jButtonReset	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonReset() {
		if (jButtonReset == null) {
			jButtonReset = new JButton();
			jButtonReset.setBounds(new Rectangle(423, 155, 70, 25));
			jButtonReset.setText("取消");
			jButtonReset.addMouseListener(new MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					NewVarGui.this.setVisible(false);
				}
			});
		}
		return jButtonReset;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(70, 64, 423, 86));
			jScrollPane.setFont(new Font("Courier New", Font.PLAIN, 12));
			jScrollPane.setViewportView(getJTextAreaRegexp());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTextAreaRegexp	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextAreaRegexp() {
		if (jTextAreaRegexp == null) {
			jTextAreaRegexp = new JTextArea();
			jTextAreaRegexp.setForeground(new Color(0, 0, 204));
		}
		return jTextAreaRegexp;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
